package com.eqdd.yiqidian.ui.gongneng.kehu;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.amap.api.maps2d.model.LatLng;
import com.eqdd.yiqidian.LianxirenInfoActivityCustom;
import com.eqdd.yiqidian.R;
import com.eqdd.yiqidian.base.Config;
import com.eqdd.yiqidian.bean.rebuild.FankuiRecordListResultBean;
import com.eqdd.yiqidian.bean.rebuild.LianxirenListResultBean;
import com.eqdd.yiqidian.bean.rebuild.slimedit.SlimEditEnterBean;
import com.eqdd.yiqidian.bean.rebuild.slimedit.SlimEditHintBean;
import com.eqdd.yiqidian.bean.rebuild.slimedit.SlimEditTimeBean;
import com.eqdd.yiqidian.http.DialogCallBack;
import com.eqdd.yiqidian.http.HttpConfig;
import com.eqdd.yiqidian.http.HttpResult;
import com.eqdd.yiqidian.map.SelectMapActivity;
import com.eqdd.yiqidian.ui.base.SlimBaseMultiEditActivity;
import com.eqdd.yiqidian.ui.select.RichTextResult;
import com.eqdd.yiqidian.ui.select.ShowRichTextActivity;
import com.eqdd.yiqidian.utils.PicUtil;
import com.eqdd.yiqidian.utils.SingleChooseUtil;
import com.eqdd.yiqidian.utils.ToastUtil;
import com.eqdd.yiqidian.widget.ActionSheetDialog;
import com.google.gson.Gson;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.config.PictureMimeType;
import com.luck.picture.lib.entity.LocalMedia;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class FankuiRecordInfoActivity extends SlimBaseMultiEditActivity {
    private LianxirenInfoActivityCustom dataBinding;
    private boolean isUpdate = false;
    private FankuiRecordListResultBean items;
    private String cusid;
    private String contacts;
    private String fankuiRecordid;
    private int fankuiType;
    private String phone;
    private Bundle bundle;
    RichTextResult richTextResult;
    private ActionSheetDialog fankuiTypeDialog;
    private String clientName;
    private String latAndLong;


    @Override
    public void initBinding() {
        dataBinding = DataBindingUtil.setContentView(this, R.layout.activity_lianxiren_info);
        initTopTitleBar(View.VISIBLE, "反馈记录");

        isSave = false;
        isRead = false;
    }


    @Override
    protected RecyclerView initRecyclerView() {
        return dataBinding.recyclerView;
    }

    @Override
    public void itemClick(int p) {
        switch (slimAdapterEx.getDataItem(p).getPosition()) {
            case 4:
                bundle = new Bundle();
                bundle.putString(Config.TITLE, "反馈图文");
                bundle.putSerializable(Config.RICH_TEXT_RESULT, richTextResult);
                startActivity(ShowRichTextActivity.class, bundle);
                break;
//            case 3:
//                if (fankuiTypeDialog == null) {
//                    SingleChooseUtil.getChooseDialog(this, 30,
//                            sheetDialog -> {
//                                fankuiTypeDialog = sheetDialog;
//                                fankuiTypeDialog.show();
//                            }
//                            , (position, strings) -> {
//                                fankuiType = position;
//                                datas.get(3).setContent(strings.get(position - 1));
//                                slimAdapterEx.notifyItemChanged(3);
//                                update();
//                            }
//
//                    );
//                } else {
//                    fankuiTypeDialog.show();
//                }
//                break;
//            case 4:
//                startActivityForResult(SelectMapActivity.class, null, Config.SELECT_MAP);
//                break;
//            case 5:
//                bundle = new Bundle();
//                bundle.putString("cusid", cusid);
//                startActivityForResult(ChooseLianxirenActivity.class, bundle, Config.CHOOSE_LIANXIREN);
//                break;

        }
    }

    @Override
    protected void onTimeClick(int position) {
        super.onTimeClick(position);
        update();
    }

    @Override
    public void setView() {
        cusid = getIntent().getStringExtra("cusid");
        clientName = getIntent().getStringExtra(Config.CLIENT_NAME);

        items = (FankuiRecordListResultBean) getIntent().getSerializableExtra("bean");
        richTextResult = new RichTextResult();
        richTextResult.setContent(items.getFbcontent());
        ArrayList<LocalMedia> localMedia = new ArrayList<>();

        List<String> lpicAddr = items.getLpicAddr();
        if (lpicAddr != null) {
            for (int i = 0, lenth = lpicAddr.size(); i < lenth; i++) {
                localMedia.add(PicUtil.localMedia(lpicAddr.get(i)));
            }
        }
        richTextResult.setSelectMedia(localMedia);
        fankuiRecordid = items.getId() + "";
        contacts = items.getFberid() + "";
        phone = items.getFberPhone();
        datas.add(new SlimEditHintBean("反馈标题", items.getFbtitle(), 0));
        datas.add(new SlimEditHintBean("反馈类型", items.getFbtype(), 1));
        datas.add(new SlimEditHintBean("反馈人", items.getFberName(), 2));
        datas.add(new SlimEditHintBean("反馈时间", items.getFbTime(), 3));
        datas.add(new SlimEditEnterBean("反馈内容", items.getFbcontent(), 4, true, "请填写"));
        datas.add(new SlimEditTimeBean("提醒时间(反馈处理信息)", items.getRemindTime(), 5, "请选择").setType(Config.YEAR_MONTH_DAY_TYPE));
        datas.add(new SlimEditHintBean("反馈地点", items.getAddr(), 6));
        datas.add(new SlimEditHintBean("创建时间", items.getCreateTime(), 7));
        slimAdapterEx.updateData(datas);
    }

    @Override
    protected void setEditBeans() {

    }

    @Override
    public void onClick(View v) {
        super.onClick(v);

    }

    public void update() {
        OkGo.<HttpResult>post(HttpConfig.BASE_URL + HttpConfig.KEHU_UPDATE_FANKUI_RECORD)
                .params("owner", user.getGuid())
                .params("cusfbRecordid", fankuiRecordid)
                .params("data", formatParam())
                .execute(new DialogCallBack<HttpResult>(FankuiRecordInfoActivity.this) {
                    @Override
                    public void onSuccess(Response<HttpResult> response) {
                        HttpResult httpResult = response.body();
                        ToastUtil.showShort(httpResult.getMsg());
                        if (httpResult.getStatus() == 200) {
                            isUpdate = true;
                        }
                    }
                });
    }

    private String formatParam() {
        HashMap<String, Object> map = new HashMap<>();
//        map.put("fbtitle", datas.get(0).getContent());
//        map.put("fbtype", datas.get(1).getContent());
//        map.put("fbcontent", datas.get(1).getContent());
//        map.put("addr", datas.get(4).getContent());
//        map.put("fberid", contacts);
//        map.put("fbTime", datas.get(6).getContent());
//        map.put("fberName", datas.get(5).getContent());
        map.put("remindTime", datas.get(5).getContent());
//        map.put("fberPhone", phone);
        return new Gson().toJson(map);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
//        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Config.INTENT_CONTENT) {
            String content = data.getStringExtra("content");
            datas.get(requestCode).setContent(content);
            slimAdapterEx.notifyItemChanged(requestCode);
            update();
        }
        if (resultCode == Config.SUCCESS) {
            switch (requestCode) {
                case Config.CHOOSE_LIANXIREN:
                    LianxirenListResultBean bean = (LianxirenListResultBean) data.getSerializableExtra("bean");
                    contacts = bean.getId() + "";
                    phone = bean.getDep();
                    slimAdapterEx.getData().get(5).setContent(bean.getName());
                    slimAdapterEx.notifyItemChanged(5);
                    update();
                    break;
                case Config.SELECT_MAP:
                    LatLng latLng = data.getParcelableExtra(Config.MAP_RESULT_LOCATION);
                    latAndLong = latLng.longitude + "," + latLng.latitude;
                    String map = data.getStringExtra(Config.MAP_RESULT_STRING);
                    datas.get(4).setContent(map);
                    slimAdapterEx.notifyItemChanged(4);
                    update();
                    break;
            }
        }
    }

    @Override
    public void onBackPressed() {
        if (isUpdate) {
            Intent intent = new Intent();
            items.setFbtitle(datas.get(0).getContent());
            items.setFbtype(datas.get(1).getContent());
            items.setFberid(Integer.parseInt(contacts));
            items.setFbTime(datas.get(6).getContent());
            items.setFbcontent(datas.get(1).getContent());
            items.setAddr(datas.get(4).getContent());
            intent.putExtra("bean", items);
            setResult(Config.SUCCESS, intent);
        }
        super.onBackPressed();

    }
}
